package com.example.simple.work.service.impl;

import com.example.simple.work.entity.Checking;
import com.example.simple.work.entity.EmployeeMessage;
import com.example.simple.work.entity.User;
import com.example.simple.work.mapper.CheckingMapper;
import com.example.simple.work.mapper.UserMapper;
import com.example.simple.work.service.ICheckingService;
import com.example.simple.work.service.IUserService;
import com.example.simple.work.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CheckingServiceImpl implements ICheckingService {
    @Autowired
    private CheckingMapper checkingMapper;


    @Override
    public void insert(Checking checking) {
        String employeeId = checking.getEmployeeId();
        if(employeeId == null || employeeId.equals("")){
            throw new UserIdNotExistException("员工工号不能为空");
        }
        // 补全数据
        checking.setCreateUser("崔阳亮");
        checking.setModifiedUser("崔阳亮");
        Date date = new Date();
        checking.setCreateTime(date);
        checking.setModifiedTime(date);

        Integer rows = checkingMapper.insert(checking);
        if(rows != 1){
            throw new InsertException("员工信息输入时存在未知错误");
        }
    }

    @Override
    public void delete(Integer id) {
        Checking result = checkingMapper.findCheckingById(id);
        if(result == null){
            throw new CheackingIdNotExistException("查询考勤表的id不存在");
        }
        Integer rows = checkingMapper.deleteById(id);
        if(rows != 1){
            throw new CheackingDeleteException("删除员工考勤时遇到了未知错误");
        }
    }

    @Override
    public List<Checking> findAllCheckingData() {
        List<Checking> list = checkingMapper.findAllChecking();
        if(list == null){
            throw new CheckingFindAllException("查询所有员工信息时遇见未知错误");
        }
        return list;
    }

    @Override
    public List<Checking> findCheckingByEmployeeName(String employeeName) {
        List<Checking> list = checkingMapper.findCheckingByEmployeeName(employeeName);
        if(list == null){
            throw new CheckingFindAllException("根据员工姓名时查询员工信息时遇见未知错误");
        }
        return list;
    }

    @Override
    public void changeChecking(Integer id, Checking checking) {
        Checking result = checkingMapper.findCheckingById(id);
        if(result == null){
            throw new CheackingIdNotExistException("考勤的id不存在，请重新输入");
        }

        // 更新修改时间
        Date date = new Date();
        checking.setModifiedTime(date);

        Integer rows = checkingMapper.update(id,checking);
        if(rows != 1){
            throw new CheckingUpdateException("修改过程中遇见了未知错误");
        }
    }

    @Override
    public void insertList(List<Checking> checkingList) {
        if(checkingList==null || checkingList.isEmpty()){
            throw new CheckingInsertListIsNotExistException("数据不能为空");
        }
        System.out.println(checkingList);
        for (Checking checking: checkingList){
            checking.setCreateUser("崔阳亮");
            checking.setModifiedUser("崔阳亮");
            Date date = new Date();
            checking.setCreateTime(date);
            checking.setModifiedTime(date);
        }
        Integer rows = checkingMapper.insertList(checkingList);
        if(rows < 1){
            throw new CheckingInsertListException("批量插入数据时出现异常");
        }
    }


}
